﻿using System;
using RedmineTypes = Redmine.Net.Api.Types;

namespace Redmine.Laptop.API.Models
{
    public class TimeEntry
    {
        public int TimeEntryID { get; set; }
        public User User { get; set; }
        public TimeEntryActivity Activity { get; set; }
        public string Comments { get; set; }
        public decimal Hours { get; set; }
        public Project Project { get; set; }
        public DateTime SpentDate { get; set; }
        public Issue Issue { get; set; }

        public static explicit operator RedmineTypes::TimeEntry(TimeEntry timeEntry)
        {
            return new RedmineTypes::TimeEntry
            {
                Id = timeEntry.TimeEntryID,
                Project = new RedmineTypes::IdentifiableName {Id = timeEntry.Project.ProjectID, Name = timeEntry.Project.ProjectName},
                Comments = timeEntry.Comments,
                User = new RedmineTypes::IdentifiableName {Id = timeEntry.User.UserID, Name = timeEntry.User.UserName},
                Hours = timeEntry.Hours,
                Activity = new RedmineTypes::IdentifiableName {Id = timeEntry.Activity.TimeEntryActivityID, Name = timeEntry.Activity.Name},
                SpentOn = timeEntry.SpentDate,
                Issue = new RedmineTypes::IdentifiableName { Id = timeEntry.Issue.IssueID, Name = timeEntry.Issue.Subject }
            };
        }

        public static explicit operator TimeEntry(RedmineTypes::TimeEntry timeEntry)
        {
            return new TimeEntry
            {
                Activity = new TimeEntryActivity {TimeEntryActivityID = timeEntry.Activity.Id, Name = timeEntry.Activity.Name},
                Project = new Project {ProjectID = timeEntry.Project.Id, ProjectName = timeEntry.Project.Name},
                User = new User {UserID = timeEntry.User.Id, FirstName = timeEntry.User.Name.Split(' ')[0], LastName = timeEntry.User.Name.Split(' ')[1]},
                Comments = timeEntry.Comments,
                Hours = timeEntry.Hours,
                SpentDate = timeEntry.SpentOn.Value,
                TimeEntryID = timeEntry.Id,
                Issue = new Issue {IssueID = timeEntry.Issue.Id, Subject = timeEntry.Issue.Name}
            };
        }
    }
}
